package com.jz.community.moduleshoppingguide.search.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpFragment;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.callback.EmptyCallback;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.R2;
import com.jz.community.moduleshoppingguide.nearshop.bean.CategoriesBean;
import com.jz.community.moduleshoppingguide.nearshop.bean.ShopDownStairsBean;
import com.jz.community.moduleshoppingguide.nearshop.presenter.NearShopPresenter;
import com.jz.community.moduleshoppingguide.nearshop.ui.NearShopView;
import com.jz.community.moduleshoppingguide.nearshop.ui.activity.NearShopActivity;
import com.jz.community.moduleshoppingguide.nearshop.ui.adapter.NearShopAdapter;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/6/4 14:38
 * 搜索楼下小店
 */
public class ShopNearFragment extends BaseMvpFragment<NearShopView.View, NearShopPresenter> implements NearShopView.View, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.rv_neighbour)
    RecyclerView rvNeighbour;
    @BindView(R2.id.sr_refresh_neighbour)
    SmartRefreshLayout srRefreshNeighbour;
    @BindView(R2.id.fragment_near_search_parent)
    LinearLayout fragmentNearSearchParent;

    private NearShopAdapter nearShopAdapter;

    private LoadService loadService;

    private String keyWord;

    private int page = 0;

    private boolean isRefresh = false;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.module_shoppingguide_fragment_sr_neighbour;
    }

    @Override
    protected NearShopPresenter createPresenter() {
        return new NearShopPresenter(this);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        handleBindShopNearAdapter();
    }

    @Override
    protected void initView() {
        super.initView();
        loadService = LoadSir.getDefault().register(fragmentNearSearchParent);
        loadService.setCallBack(EmptyCallback.class, (context, view) -> {
            ImageView empty_iv = view.findViewById(R.id.empty_img);
            TextView empty_tv = view.findViewById(R.id.empty_tv);
            empty_iv.setImageResource(R.mipmap.empty_default);
            empty_tv.setText("未找到相关搜索结果，换个词试试！");
        });
    }

    public void setKeyWord(String searchKeyWord) {
        keyWord = searchKeyWord;
    }

    @Override
    protected void initLazy(@Nullable Bundle savedInstanceState) {
        super.initLazy(savedInstanceState);
        getData();
    }

    private void getData() {
        mPresenter.showShopDownStairs(page, 10, BaseSpUtils.getInstance().getCurrentLon(getActivity()), BaseSpUtils.getInstance().getCurrentLat(getActivity()), keyWord, "", "3000", 1, 1);
    }

    private void handleBindShopNearAdapter() {
        rvNeighbour.setLayoutManager(new LinearLayoutManager(getActivity()));
        srRefreshNeighbour.setOnRefreshListener(this);
        nearShopAdapter = new NearShopAdapter(R.layout.module_shoppingguide_item_near_shop_main, new ArrayList<>());
        nearShopAdapter.setEnableLoadMore(false);
        nearShopAdapter.setOnLoadMoreListener(this, rvNeighbour);
        rvNeighbour.setAdapter(nearShopAdapter);
        nearShopAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getContext(), NearShopActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void shopCategorys(CategoriesBean categoriesBean) {

    }

    public void changeSearch(String content) {
        keyWord = content;
        page = 0;
        if (null != nearShopAdapter) {
            nearShopAdapter.replaceData(new ArrayList<>());
        }
        getData();
    }

    @Override
    public void shopDownStairsData(ShopDownStairsBean shopDownStairsBean, int type) {
        if (!Preconditions.isNullOrEmpty(shopDownStairsBean.get_embedded()) && !Preconditions.isNullOrEmpty(shopDownStairsBean.get_embedded().getShopDownStairsViews())) {
            srRefreshNeighbour.finishRefresh();
            loadService.showSuccess();
            for (int i = 0; i < shopDownStairsBean.get_embedded().getShopDownStairsViews().size(); i++) {
                List<ShopDownStairsBean.EmbeddedBeanX.ContentBean.EmbeddedBean.GoodsInfoViewListBean> listBean = shopDownStairsBean.get_embedded().getShopDownStairsViews().get(i).get_embedded().getGoodsInfoViewList();
                if (!Preconditions.isNullOrEmpty(listBean)) {
                    listBean.add(listBean.size(), new ShopDownStairsBean.EmbeddedBeanX.ContentBean.EmbeddedBean.GoodsInfoViewListBean());
                }
            }
            setData(isRefresh, shopDownStairsBean.get_embedded().getShopDownStairsViews(), shopDownStairsBean);

        } else {
            loadService.showCallback(EmptyCallback.class);
        }
    }

    private void setData(boolean isRefresh, List data, ShopDownStairsBean shopDownStairsBean) {

        page++;

        if (isRefresh) {
            nearShopAdapter.addData(data);
        } else {
            nearShopAdapter.setNewData(data);
        }

        if (shopDownStairsBean.getPage().getTotalPages() <= page) {
            nearShopAdapter.loadMoreEnd();
        } else {
            nearShopAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showErro(String s, int type) {

    }


    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        page = 0;
        isRefresh = false;
        getData();
    }

    @Override
    public void onLoadMoreRequested() {
        isRefresh = true;
        getData();
    }
}
